<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>02-倒计时效果</title>
</head>
<body>
    <script>
  /* 
    倒计时效果
    1. 获取两个时间点（构造函数）
    2. 转化为时间戳，并相减
    3. 结果转化为时分秒
  */
      
  /*   转化公式
      d = parseInt(总秒数 / 60 /60 /24);
      h = parseInt(总秒数 / 60 / 60 % 24);
      m = parseInt(总秒数 / 60 % 60);
      s = parseInt(总秒数 % 60); */
     
      // 某个时间到当前的倒数计时
      function countDown(date){
          let date1 = Date.now();
          let date2 = date.valueOf();
        //   console.log(date);  // 获取目标时间

          let sub = date2 - date1;
          sub = sub / 1000;

          h = parseInt(sub / 60 / 60 % 24);
          m = parseInt(sub / 60 % 60);
          s = parseInt(sub % 60);

          h = h<10 ? '0' + h : h;
          m = m<10 ? '0' + m : m;
          s = s<10 ? '0' + s : s;

          return h + ',' + m + ',' + s;
      }
      let result = countDown(new Date('2022-07-04 18:00:00'));
      console.log(result);
    </script>
</body>
</html>